package com.harmontronics.hanma.paas.paas.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.annotation.Log;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.domain.app.entity.valueobject.PageTypeEnum;
import com.harmontronics.hanma.paas.paas.dto.input.HmPageCopyInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmPageJsonInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmPageAppService;
import com.harmontronics.hanma.paas.paas.dto.output.HmPageResult;
import com.harmontronics.hanma.paas.paas.dto.input.HmPageCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmPageUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.app.entity.HmPage;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 流程表单
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-01 15:11:16
 */
@RestController
@RequestMapping("/api/hm-page")
@Api(tags = {"流程表单"})
public class HmPageController {
    @Autowired
    private HmPageAppService hmPageAppService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmPageResult>> list(@ApiParam("模型ID")@RequestParam Long modelId,
                                                   @ApiParam("页面类型")@RequestParam(required = false) PageTypeEnum type,
                                                   @ApiParam("过滤未配置JSON的记录")@RequestParam(required = false) Boolean filterNonJson) {
        List<HmPage> hmPageList = hmPageAppService.getList(modelId,type,filterNonJson);

        return ResponseEntity.ok(Convert.toList(HmPageResult.class, hmPageList));
    }


    @GetMapping("list/all")
    @ApiOperation("列表")
    public ResponseEntity<List<HmPageResult>> listAll(
                                                   @ApiParam("过滤未配置JSON的记录")@RequestParam(required = false) Boolean filterNonJson) {
        List<HmPage> hmPageList = hmPageAppService.getListAll(filterNonJson);

        return ResponseEntity.ok(Convert.toList(HmPageResult.class, hmPageList));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmPageResult>> list(
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页面类型") @RequestParam(required = false) PageTypeEnum type,
            @ApiParam(value = "页码", defaultValue = "1")@RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        PageBase<HmPage> hmFlowableFormPageBase = hmPageAppService.getList(type,name, pageNo, pageSize);
        PageBase<HmPageResult> resultPageBase = PageBase.copy(hmFlowableFormPageBase, HmPageResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("详情")
    public ResponseEntity<HmPageResult> info(@RequestParam("id") Long id,@RequestParam(required = false) Boolean includeJson) {
        HmPage hmPage = hmPageAppService.findById(id,includeJson);
        HmPageResult result = BeanUtil.toBean(hmPage, HmPageResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 信息
     */
    @GetMapping("/info/key")
    @ApiOperation("详情")
    public ResponseEntity<HmPageResult> infoByKey(@RequestParam("key") String key,@RequestParam(required = false) Boolean includeJson) {
        HmPage hmPage = hmPageAppService.findByKey(key,includeJson);
        HmPageResult result = BeanUtil.toBean(hmPage, HmPageResult.class);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    @Log("新增了页面")
    public ResponseEntity<Long> save(@Valid @RequestBody HmPageCreateInput hmPageCreateInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmPage hmPage = BeanUtil.toBean(hmPageCreateInput, HmPage.class);
        Long id = hmPageAppService.save(hmPage);

        return ResponseEntity.ok(id);
    }


    /**
     * 保存
     */
    @PostMapping("copy")
    @ApiOperation("复制页面")
    @Log("复制了页面")
    public ResponseEntity<Long> copy(@Valid @RequestBody HmPageCopyInput hmPageCopyInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);

        Long id = hmPageAppService.copy(hmPageCopyInput.getPageId(),hmPageCopyInput.getName(),hmPageCopyInput.getDescription());

        return ResponseEntity.ok(id);
    }



    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    @Log("修改了页面")
    public ResponseEntity<String> update(@Valid @RequestBody HmPageUpdateInput hmPageUpdateInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmPage hmPage = BeanUtil.toBean(hmPageUpdateInput, HmPage.class);
        hmPageAppService.update(hmPage);
        return ResponseEntity.ok();
    }


    /**
     * 修改配置信息
     */
    @ApiOperation("修改配置信息")
    @PutMapping("design")
    @Log("修改了页面配置信息")
    public ResponseEntity<String> updateJson(@Valid @RequestBody HmPageJsonInput hmPageJsonInput,BindingResult bindingResult){
        ParamUtils.valid(bindingResult);
        hmPageAppService.updateJson(hmPageJsonInput.getId(),hmPageJsonInput.getJson());
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    @Log("删除了页面")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmPageAppService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }


    @GetMapping("valid-field")
    @ApiOperation("字段重复检查")
    public ResponseEntity<String> validField(@RequestParam String key, @RequestParam String value, @RequestParam(required = false) Long id,@RequestParam PageTypeEnum typeEnum) {
        hmPageAppService.validField(id, key, value,typeEnum);
        return ResponseEntity.ok();
    }
}
